package com.bike.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bike.controller.BaseController;
import com.bike.entity.SysRole; 
import com.bike.util.PageData;
import com.bike.service.impl.SysRoleServiceImpl;

 
@Controller
@RequestMapping(value="/sysRole")
public class SysRoleController extends BaseController {
	
		@Autowired
		private SysRoleServiceImpl  sysRoleService;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/system/role/sysRole_list";
		}
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toChooseTree")
		public String toChooseTree(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd",pd);
			return "/system/role/menu_choosetree";
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysRoleService.selectSysRoleList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysRoleService.selectSysRoleList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			if(pd.getLong("roleId")>0){
				model.addAttribute("pd", sysRoleService.selectBySysRoleId(pd.getLong("roleId")));
			} 
			return "/system/role/sysRole_edit";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,SysRole sysRole){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				sysRoleService.saveOrUpdate(pd,sysRole);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 新增和修改 
		 */
		@RequestMapping("saveqx")
		@ResponseBody
		public Map<String,Object> saveqx(HttpServletRequest request,SysRole sysRole){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				sysRoleService.saveRights(pd);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("roleId")>0){
					sysRoleService.deleteSysRoleById(pd.getLong("roleId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
	 	/**
	 	 * 取消授权
	 	 * @param request
	 	 * @return
	 	 */
	 	@RequestMapping("unBingRight")
	 	@ResponseBody
	 	public Map<String,Object> unBingRight(HttpServletRequest request){
	 		try {
	 			PageData pd=PageData.getPageDataByPage(request);
	 			if(pd.getLong("roleId")>0){
	 				sysRoleService.bingRight(pd,1);
	 				return getSuccessMap();
	 			}
	 		} catch (Exception e) {
	 			e.printStackTrace();
	 		}
	 		return getErrorMap("失败");
	 	}
	 	/**
	 	 * 授权
	 	 * @param request
	 	 * @return
	 	 */
	 	@RequestMapping("bingRight")
	 	@ResponseBody
	 	public Map<String,Object> bingRight(HttpServletRequest request){
	 		try {
	 			PageData pd=PageData.getPageDataByPage(request);
	 			if(pd.getLong("roleId")>0){
	 				sysRoleService.bingRight(pd,0);
	 				return getSuccessMap();
	 			}
	 		} catch (Exception e) {
	 			e.printStackTrace();
	 		}
	 		return getErrorMap("失败");
	 	}
}
